// pages/goYuyue/goYuyue.js
const app = getApp()

const {
  type
} = require("../../../config.js");
var common = require("../../../public.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDoudong: false, //控制图片抖动
    active: 0,
    show: false,
    detail: {},
    checked: true,
    newResData: '',
    cardProductType: "",
  },
  // 打开弹窗
  onClickShow() {
    this.setData({
      show: true
    });
  },
  onClickHide() {

    this.setData({
      show: false
    }, () => {
      // 跳转预约界面
      wx.navigateTo({
        url: '/pages/goYuyue/yuYue/index',
      })
    });
  },
  noop() {},
  // 标签切换
  onChange(event) {
    this.setData({
      checked: !this.data.checked
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  goBack() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; //上一个页面
    // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
    if (prevPage) {
      console.log(prevPage)
      prevPage.setData({
        actives: this.data.productType, // 修改上一页的属性值；
      })

      wx.navigateBack({
        delta: 1
      })

    } else {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }
  },
  onLoad: function (options) {
    var that = this
    //获取胶囊位置
    let capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
        height: capsule.height,
        top: capsule.top,
        storeId: options.id,
    })
    var windowHeight = wx.getSystemInfoSync().windowHeight
    var windowWidth = wx.getSystemInfoSync().windowWidth
    this.setData({
        scroll_height: windowHeight * 750 / windowWidth - 200
    })

    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token,
          productId: options.productId ? options.productId : '',
          cardId: options.cardId ? options.cardId : '',
          productType: options.productType ? options.productType : '',
          cardProductType: options.cardProductType ? options.cardProductType : ''
        }, function () {
          this.cardsList_two() //会员卡列表
        })
      } else {
        that.setData({
          userInfo: null,
          token: '',
        })
      }
    }, true)
  },
  // 会员卡详情下面的列表会员卡可享权益
  cardsList_two() {
    var that = this
    common.get('/userAnnualCard/getCardProductListForApplets', {
      cardId: that.data.cardId,
      productType: that.data.productType,
      userId: that.data.userInfo.id,
      token: that.data.token
    }, res => {
      if (res.status === 200) {
        var time = res.data.find(item => item.productId == that.data.productId)
        console.log(time, '到了')
        var jsonDa = JSON.stringify(time.enterInvoiceExplain).replace(/<img/gi, "<img class='richImg'style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
       
        //遍历得到的数据，给img添加CSS和style
        var newResData = JSON.parse(jsonDa);
        that.setData({
          detail: time,
          newResData,
          html: time.enterInvoiceExplain
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  newResData() {
    console.log("点击预览")
  },

  toBookPage() { // 立即预订
    var that = this
    let isDoudong = this.data.isDoudong
    if (!this.data.checked) {
      wx.showToast({
        title: '请勾选预约须知',
        icon: 'none',
        success() {
          that.setData({
            isDoudong: true
          }, () => {
            setTimeout(() => {
              that.setData({
                isDoudong: false
              })
            }, 1000)
          })
        }
      })
      return
    }
    var that = this

    // 判断是否是会员卡商品
    if (that.data.cardProductType == 1) {
      console.log(that.data.cardProductType + "61452252525")
      wx.navigateTo({
        url: '/packageA/pages/Goods/Commit/index?storeId=' + this.data.detail.storeId + '&itemId=' + this.data.detail.productId +
          '&cardId=' + this.data.detail.cardId +
          '&special=1',
      })
     
    }else if(that.data.cardProductType == 2){ //优惠券商品
      console.log(this.data.detail.productId,"this.data.detail.productId")
      wx.navigateTo({
        url: '/packageA/pages/Goods/CommitBenifit/index?storeId=' + this.data.detail.storeId + '&itemId=' + this.data.detail.productId +
          '&cardId=' + this.data.detail.cardId +
          '&productId=' + this.data.detail.productId +
          '&detail=' + JSON.stringify(this.data.detail)
      })
     
    }else {
      wx.navigateTo({
        url: '/pages/ScenicArea/ScenicAreaChooseTickets/index?ticketId=' + that.data.detail.productId +
          '&storeId=' + this.data.detail.storeId +
          '&ticketName=' + that.data.detail.productName +
          '&storeName=' + this.data.detail.storeName +
          '&cardId=' + this.data.detail.cardId +
          '&discount=' + this.data.detail.discount +
          '&pid_name=' + this.data.detail.pid_name +
          '&bookFlag=' + this.data.detail.bookFlag +
          '&bookPreDays=' + this.data.detail.bookPreDays
      })
    }
  },

})